#!/usr/bin/python
import os
import sys
import shutil
import zipfile


szPrompt = "Version number: "
szExportDir = "export"
szCopyDir = "GerikSpellsDemo"
szBaseRarFilename = ".BaseRarFile.rar"
szBaseRarFile = os.path.join(szExportDir, szBaseRarFilename)

szVersion = raw_input(szPrompt)
szRarFilename = "GerikSpellsDemo-v%s.rar" % szVersion
szRarFile = os.path.join(szExportDir, szRarFilename)
szExportCopyDir = os.path.join(szExportDir, szCopyDir)

# Test if file is open
if os.path.isfile(os.path.join(szRarFile)):
    print ""
    print "ERROR: File already exists.... exiting."
    raw_input()
    sys.exit()

# Make the rar file to be used.
shutil.copyfile(szBaseRarFile, szRarFile)

os.mkdir(os.path.join(szExportDir, szCopyDir))
    
# Walk through directory adding files to the new directory.

for root, dirs, files in os.walk(os.path.join("..", szCopyDir)):
    if ".svn" in dirs:
        dirs.remove(".svn")

    for directory in dirs:
        fullDirName = os.path.join(root, directory)
        os.mkdir(os.path.join(os.path.join(szExportDir, szCopyDir[3:]), fullDirName))

    for filename in files:
        print "'%s', '%s'" % (root, filename)
        fullFilename = os.path.join(root, filename)
        shutil.copyfile(fullFilename, os.path.join(szExportDir, os.path.join(root[3:], filename)))

szURL = "http://civ4-gerikspells.googlecode.com/svn/trunk/SDK%20Source/CvGameCoreDLL_v161/CvGameCoreDLL/"
szCommand = "svn diff --diff-cmd diff -x -b -r 4 %s > diff.txt" % szURL

os.system(szCommand)

diffFile = open("diff.txt", "r")

szSDKRarFile = "GerikSpellsSDKChanges-v%s.rar" % szVersion

shutil.copyfile(szBaseRarFile, os.path.join(szExportDir, szSDKRarFile))

sourceDir = os.path.join("..", "SDK Source", "CvGameCoreDLL_v161", "CvGameCoreDLL")

exportSDKChangeDir = os.path.join("export", "CvGameCoreDLL")
os.makedirs(exportSDKChangeDir)


for line in diffFile.readlines():
    if line.find("Index: ") == 0:
        name = line.split("Index: ")[1].replace("\n", "")
        shutil.copyfile(os.path.join(sourceDir, name), os.path.join(exportSDKChangeDir, name))

diffFile.close()

shutil.move("diff.txt", os.path.join(exportSDKChangeDir, "diff.txt"))





# Print end message
print ""
print "Creation complete."
raw_input()


    
